package work3.student;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 *
 *  根据用户输入的选择去实现增加、删除、修改、查找以及遍历所有学生信息
 *
 * */
public class StudentModel {
    private static List<StudentInfo> mStudentList;
    private static Scanner mScanner;
    //传入所有学生数据
    public static void setStudentList(List<StudentInfo> list){
        mStudentList = list;
        mScanner = new Scanner(System.in);
    }
    public static void searchStudent(){

        System.out.println("请选择查找方式：");
        System.out.println("1:学号查询");
        System.out.println("2:姓名查询");
        switch (mScanner.nextInt()) {
            case 1:
                System.out.println("请输入学号");
                int no = mScanner.nextInt();
                searchStudentByNo(no);
                break;
            case 2:
                System.out.println("请输入姓名");
                String name = mScanner.next();
                searchStudentByName(name);
                break;
        }
    }
    //根据姓名查找学生  （为了防止出现同名同姓,返回使用list）
    public static List<StudentInfo> searchStudentByName(String name){
        List<StudentInfo> list = new ArrayList<>();
        for (int i = 0; i < mStudentList.size(); i++) {
            if (mStudentList.get(i).getName().equals(name))
                list.add(mStudentList.get(i));
        }
        if (list.size() != 0){
            System.out.println("查询到的学生信息为:");
            for (int i = 0; i < list.size(); i++) {
                System.out.println( list.get(i).toString());
            }
        } else {
            System.out.println("未查询到相应信息");
        }
        return list;
    }
    //根据学号查找学生
    public static void searchStudentByNo(int no){
        boolean hasStudent = false;
        for (int i = 0; i < mStudentList.size(); i++) {
            if (mStudentList.get(i).getStudentNo() == no){
                hasStudent = true;
                System.out.println("查询到的学生信息为:");
                System.out.println( mStudentList.get(i).toString());
                break;
            }

        }
        if (!hasStudent)System.out.println("未查询到相应信息");
    }

    // 打印所有学生列表
    public static void printStudentInfo(){
        for (int i = 0; i < mStudentList.size(); i++) {
            System.out.println(mStudentList.get(i).toString());
        }
    }

    //修改学生信息
    public static void updateStudentInfo(){
        //因为学号是唯一变量，所以修改和删除都使用学号进行
        System.out.println("请输入需要修改的学生的学号:");

        boolean hasStudent = false;
        StudentInfo info = null;
        int no = mScanner.nextInt();
        for (int i = 0; i < mStudentList.size(); i++) {
            if (mStudentList.get(i).getStudentNo() == no){
                hasStudent = true;
                System.out.println("查询到的学生信息为:");
                System.out.println( mStudentList.get(i).toString());
                info = mStudentList.get(i);
                break;
            }
        }
        if (hasStudent){
            updateInfo(info);
        } else {
            System.out.println("未查询到相应信息");
        }

    }
    private static void updateInfo(StudentInfo info){
        System.out.println("您需要修改该学生什么信息:");
        System.out.println("1:姓名");
        System.out.println("2:学号");
        System.out.println("3:年龄");
        System.out.println("4:结束");
        switch (mScanner.nextInt()) {
            case 1:
                System.out.println("修改的姓名为：");
                info.setName(mScanner.next());
                updateInfo(info);
            break;
            case 2:
                System.out.println("修改的学号为：");
                int no = mScanner.nextInt();
                boolean hasNo = false;
                for (int i = 0; i < mStudentList.size(); i++) {
                    if (mStudentList.get(i).getStudentNo() == no)
                    {
                        hasNo = true;
                        break;
                    }
                }
                if (hasNo){
                    System.out.println("学生学号已存在");
                } else {
                    info.setStudentNo(no);
                }
                updateInfo(info);
                break;
            case 3:
                System.out.println("修改的年龄为：");
                int age = mScanner.nextInt();
                info.setAge(age);
                updateInfo(info);
                break;
            case 4:
                break;
            default:
                System.out.println("无效指令");
                updateInfo(info);
                break;
        }

    }
    //增加学生信息
    public static void addStudent(){

        System.out.println("请输入需要增加的学生信息");
        System.out.println("学号：");

        int studentNo = mScanner.nextInt();
        boolean hasNo = false;
        for (int i = 0; i < mStudentList.size(); i++) {
            if (mStudentList.get(i).getStudentNo() == studentNo)
            {
                hasNo = true;
                break;
            }
        }
        if (hasNo){
            System.out.println("学生学号已存在，输入1请重新添加，输入2退出添加");
            int i = mScanner.nextInt();
            switch (i) {
                case 1:
                    addStudent();
                    return;
                case 2:
                  return;
            }
        }
        System.out.println("年龄：");
        int age = mScanner.nextInt();
        System.out.println("姓名:");
        String name = mScanner.next();

        try {
            StudentInfo info = new StudentInfo(studentNo,name,age);
            mStudentList.add(info);
            System.out.println("学生添加成功");
        } catch (StudentNoException e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        } catch (AgeException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }


    }
    //删除学生信息
    public static void delStudent(){
        System.out.println("请输入需要删除的学生的学号:");
        boolean hasStudent = false;
        StudentInfo info = null;
        int no = mScanner.nextInt();
        for (int i = 0; i < mStudentList.size(); i++) {
            if (mStudentList.get(i).getStudentNo() == no){
                hasStudent = true;
                System.out.println("查询到的学生信息为:");
                System.out.println( mStudentList.get(i).toString());
                info = mStudentList.get(i);
                break;
            }
        }
        if (!hasStudent){
            System.out.println("未查询到相应信息");
        } else {
            if (mStudentList.remove(info)) {
                System.out.println("删除成功");
            } else {
                System.out.println("删除失败");
            }
        }
    }


}
